package com.lzj.demo.design.common;


import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author : dengbin
 * @Date: 2020/9/5
 * 统一的返回结果实体
 */
public class ServerResult<T> implements Serializable {
    /**
     * 序列化ID
     */
    private static final long serialVersionUID = -5809782578272943999L;
    /**
     * 返回的状态码
     */
    private int result = ServerResultCode.SUCCESS.getErrorCode();
    /**
     * 返回的消息
     */
    private String message = ServerResultCode.SUCCESS.getErrorDesc();
    /**
     * 返回的数据实体
     */
    private T data = null;
    /**
     *
     */
    final int INIT_SIZE = 128;

    /**
     *
     */
    public ServerResult() {
    }

    @Override
    public String toString() {
        StringBuilder buffer = new StringBuilder(INIT_SIZE);
        buffer.append("{\"result\":").append(this.result);
        buffer.append(",\"message\":\"").append(this.message != null ? message : "").append("\"");
        buffer.append(",\"data\":").append(data).append("}");
        return buffer.toString();
    }

    public int getResult() {
        return result;
    }

    public ServerResult setResult(int result) {
        this.result = result;
        return this;
    }

    public ServerResult setResult(ServerResultCode serverResultCode) {
        this.result = serverResultCode.getErrorCode();
        this.message = serverResultCode.getErrorDesc();
        return this;
    }

    public ServerResult setResult(int errorCode, String errorDesc) {
        this.result = errorCode;
        this.message = errorDesc;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public ServerResult<T> setMessage(String message) {
        this.message = message;
        return this;
    }

    public T getData() {
        return data;
    }

    public ServerResult setData(T data) {
        this.data = data;
        return this;
    }

    public static <T> ServerResult<T> success() {
        return new ServerResult()
                .setResult(ServerResultCode.SUCCESS)
                .setMessage(ServerResultCode.SUCCESS.getErrorDesc())
                .setData(defaultResult(ServerResultCode.SUCCESS));
    }

    public static <T> ServerResult<T> success(T data) {
        return new ServerResult()
                .setResult(ServerResultCode.SUCCESS)
                .setMessage(ServerResultCode.SUCCESS.getErrorDesc())
                .setData(data);
    }

    public static <T> ServerResult<T> success(T data, String message) {
        return new ServerResult()
                .setResult(ServerResultCode.SUCCESS)
                .setMessage(message)
                .setData(data);
    }

    /**
     * @description 错误码使用系统错误 1000 错误类型自定义
     * @author dengbin
     * @date 2020/9/12
     */
    public static <T> ServerResult<T> fail(String message) {
        return new ServerResult()
                .setResult(ServerResultCode.FAIL)
                .setMessage(message)
                .setData(defaultResult(null));
    }

    /**
     * 错误时需要返回相关数据时使用
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ServerResult<T> fail(T data, ServerResultCode serverResultCode){
        return new ServerResult()
                .setData(data)
                .setResult(serverResultCode)
                .setMessage(serverResultCode.getErrorDesc());
    }

    /**
     * 错误时需要返回相关数据时使用(不设值 data)
     * @param sr
     * @param
     * @return
     */
    public static <T> ServerResult<T> fail(ServerResult<?> sr){
        return new ServerResult<>()
                .setMessage(sr.getMessage())
                .setResult(sr.getResult());

    }
    /**
     * @description 错误码使用上面的枚举定义的，错误信息重新定义
     * @author dengbin
     * @date 2020/9/12
     */
    public static <T> ServerResult<T> fail(ServerResultCode serverResultCode, String msg) {
        return new ServerResult()
                .setResult(serverResultCode)
                .setMessage(msg)
                .setData(defaultResult(null));
    }

    /**
     * @description 直接选用上面的枚举错误码，输出定义的错误信息
     * @author dengbin
     * @date 2020/9/12
     * @return
     */
    public static <T> ServerResult<T> fail(ServerResultCode serverResultCode) {
        return new ServerResult()
                .setResult(serverResultCode)
                .setMessage(serverResultCode.getErrorDesc())
                .setData(defaultResult(null));
    }

    /**
     * 错误时需要返回相关数据时使用
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ServerResult<T> fail(T data, int errorCode,String errorDesc){
        return new ServerResult()
                .setData(data)
                .setResult(errorCode,errorDesc);
    }


    public static <T> ServerResult<T> fail(){
        return new ServerResult()
                .setData(defaultResult(null));
    }


    /**
     * @description 直接选用上面的枚举错误码，输出定义的错误信息
     * @author dengbin
     * @date 2020/9/12
     */
    public ServerResult failEnum(ServerResultCode serverResultCode) {
        setResult(serverResultCode);
        setMessage(serverResultCode.getErrorDesc());
        return this;
    }

    public static <T> ServerResult<T> fail(int errorCode, String errorDesc) {
        return new ServerResult()
                .setResult(errorCode, errorDesc)
                .setData(defaultResult(null));

    }

    /**
     * 判断当前的状态码是否是SUCCESS
     * @return 结果 ture or false
     */
    public boolean checkSuccess() {
        return result == ServerResultCode.SUCCESS.getErrorCode();
    }

    public boolean checkNotSuccess(){
        return !checkSuccess();
    }

    private static Map<String, Object> defaultResult(Object object){
        Map<String,Object> map = new HashMap<>(1);
        map.put("result",null);
        return map;
    }
}
